<?php
if ($ticket_parent!=1) {
	header('Location: class_manager.php');
	exit;
}
?>

<script type="text/javascript">
	function expand_collapse_div(element_id){
		var t=document.getElementById(element_id);
		if(!t)return false;
		if(t.style.display=="none"){
			t.style.display="block";
		}
		else{
			t.style.display="none";
		}
		return true;
	}
</script>





<?php 
$names_result = mysql_query("SELECT name FROM completed where forum_member_id='".$_SESSION["user"]["member_id"]."' group by name", $class_manager_db) or die(mysql_error($class_manager_db));
if(mysql_num_rows($names_result)>0){
?>
	<div class="present">
	<h2 class="spaced">Completed</h2>
	<table class="present">
	<?php 
	while($names_line = mysql_fetch_array($names_result, MYSQL_ASSOC)) {
	?>
		<tr>
			<td align="right" width="100">
				<?php echo htmlspecialchars(stripslashes($names_line["name"]))?>:
			</td>
			<td>
				<?php 
				$completed_result = mysql_query("select completed.forum_member_id, completed.name, grouped_class_w_startdate_w_mod_name.short_name, UNIX_TIMESTAMP(grouped_class_w_startdate_w_mod_name.start_datetime) as start_datetime from completed left join (select grouped_class_w_startdate.classes_pkey, grouped_class_w_startdate.modules_pkey, grouped_class_w_startdate.start_datetime, modules.short_name from (select * from (select classes.pkey as classes_pkey, classes.module as modules_pkey, sessions.start_datetime from classes left join sessions on classes.pkey=sessions.class order by start_datetime asc) class_w_startdate group by classes_pkey) grouped_class_w_startdate LEFT JOIN modules ON grouped_class_w_startdate.modules_pkey=modules.pkey) grouped_class_w_startdate_w_mod_name on completed.class=grouped_class_w_startdate_w_mod_name.classes_pkey where completed.forum_member_id='".$_SESSION["user"]["member_id"]."' AND completed.name='".$names_line["name"]."'", $class_manager_db) or die(mysql_error($class_manager_db));
				$i=0;
				while($completed_line = mysql_fetch_array($completed_result, MYSQL_ASSOC)) {
					if($i>0)
						echo ", ";
					echo htmlspecialchars(stripslashes($completed_line["short_name"])).date("dmY",$completed_line["start_datetime"]);
					$i++;
				}
				?>
			</td>
		</tr>
	<?php 
	}
	?>
	</table>
	</div>
<?php 
}
?>





<div class="present">
<h2 class="spaced">Upcoming Classes</h2>
<?php 
$result = mysql_query("select participants.pkey as participants_pkey, participants.character_name, participants.status, participants.alumnus, participants.payment_code, ordered_classes_modules.classes_pkey, ordered_classes_modules.capacity, ordered_classes_modules.fee, ordered_classes_modules.comments, UNIX_TIMESTAMP(ordered_classes_modules.start_datetime) as start_datetime, ordered_classes_modules.modules_pkey, ordered_classes_modules.short_name, ordered_classes_modules.long_name   from     participants   left join     (      select ordered_classes.display_until, ordered_classes.pkey as classes_pkey, ordered_classes.reg_start, ordered_classes.capacity, ordered_classes.fee, ordered_classes.comments, ordered_classes.start_datetime, modules.pkey as modules_pkey, modules.short_name, modules.long_name       from         (  select * from (select grouped_class_w_startdate.pkey, grouped_class_w_startdate.module, grouped_class_w_startdate.reg_start, grouped_class_w_startdate.capacity, grouped_class_w_startdate.fee, grouped_class_w_startdate.comments, grouped_class_w_startdate.start_datetime, DATE_ADD(sessions.start_datetime, INTERVAL (sessions.duration + 1) HOUR) as display_until from (select * from (select classes.pkey as pkey, classes.module, classes.reg_start, classes.capacity, classes.fee, classes.comments, sessions.start_datetime from classes left join sessions on classes.pkey=sessions.class order by start_datetime asc) class_w_startdate group by pkey) grouped_class_w_startdate left join sessions on grouped_class_w_startdate.pkey=sessions.class order by sessions.start_datetime desc) grouped_class_w_startdate_w_display_until group by pkey        ) ordered_classes       left join         modules       on ordered_classes.module=modules.pkey    ) ordered_classes_modules     on participants.class=ordered_classes_modules.classes_pkey    where participants.forum_member_id='".$_SESSION["user"]["member_id"]."'   and ordered_classes_modules.display_until >= NOW()   order by start_datetime desc", $class_manager_db) or die(mysql_error($class_manager_db));
if(mysql_num_rows($result) <= 0){
	echo '<div style="text-align:center;">You have not signed up for any upcoming classes, check them out <a title="Upcoming Classes" href="class_manager.php?page=upcoming">here</a>.</div>';
}
else{
?>
Click a class to expand it for details.
<table>
<?php 
$i=0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$display_status = "";
	if($line["alumnus"]==1)
		$display_status = "Alumnus, ";
	
	if($line["status"]=="wait_list")
		$display_status .= "wait listed";
	else
		$display_status .= $line["status"];
	
	if($line["start_datetime"] == NULL)
		$class_header=htmlspecialchars(stripslashes($line["character_name"]))." - ".htmlspecialchars(stripslashes($line["long_name"]))." (???) - ".$display_status;
	else
		$class_header=htmlspecialchars(stripslashes($line["character_name"]))." - ".htmlspecialchars(stripslashes($line["long_name"]))." (".date("d M Y",$line["start_datetime"]).") - ".$display_status;

?>
	<tr><td colspan="3"><hr></td></tr>
	<tr><td colspan="3">
		<h3><a onclick="expand_collapse_div('class_details_<?php echo $i;?>')" class="clickable">
			<?php echo $class_header;?>
		</a></h3>
	</td></tr>
	<tr><td colspan="3">
		<div id="class_details_<?php echo $i;?>" style="display:none;">







			<table class="present">
				<tr>
					<td align="right">
						Current status: 
					</td>
					<td>
						<?php 
							if ($line["status"] == "wait_list"){
								$wait_list_result = mysql_query("SELECT forum_member_id, character_name, timestamp FROM participants WHERE class='".$line["classes_pkey"]."' AND status='wait_list' ORDER BY timestamp ASC", $class_manager_db) or die(mysql_error($class_manager_db));
								$pos=0;
								while($wait_list_line = mysql_fetch_array($wait_list_result, MYSQL_ASSOC)) {
									$pos++;
									if($wait_list_line["forum_member_id"]==$_SESSION["user"]["member_id"] && $wait_list_line["character_name"]==$line["character_name"])
										break;
								}
								echo "<b>wait listed</b>, you are number <b>".$pos."</b> on the wait list";
							}
							else{
								echo "<b>".htmlspecialchars($line["status"])."</b>";
							}
						?>
					</td>
					<td align="right">
						<?php 
						if ($line["status"] != "withdrawn"){
						?>
							<form name="withdraw_class_form" action="class_manager.php?page=my_classes" method="post">
							<input type="hidden" name="withdraw_class_form_participant" value="<?php echo $line["participants_pkey"];?>">
							<input type="submit" name="withdraw_class_form_submit" value="Withdraw">
							</form>
						<?php
						}
						else{
						?>
							<form name="reinstate_class_form" action="class_manager.php?page=my_classes" method="post">
							<input type="hidden" name="reinstate_class_form_participant" value="<?php echo $line["participants_pkey"];?>">
							<input type="submit" name="reinstate_class_form_submit" value="Reinstate">
							</form>
						<?php
						}
						?>
					</td>
				</tr>
<?php 
if ($line["status"] != "alumnus" && isset($line["payment_code"])){
?>
				<tr>
					<td align="right">
						Paid: 
					</td>
					<td colspan="2">
						<?php
							$amount_paid = 0;
							$amount_paid_result = mysql_query("SELECT SUM(amount) as amount FROM transactions WHERE payment_code='".$line["payment_code"]."'", $class_manager_db) or die(mysql_error($class_manager_db));
							if(mysql_num_rows($amount_paid_result)>0){
								$amount_paid_line = mysql_fetch_array($amount_paid_result, MYSQL_ASSOC);
								$amount_paid = $amount_paid_line["amount"];
							}
							if($amount_paid<$line["fee"])
								$amount_class="red";
							else
								$amount_class="green";
								
							echo '<span class="'.$amount_class.'">'.number_format($amount_paid,2,".",",").'</span> of '.number_format($line["fee"],2,".",",").' ISK';
						?>
					</td>
				</tr>
	<?php 
	if ($line["status"] == "registered"){
	?>
				<tr>
					<td align="right" valign="top">
						Payment code:
					</td>
					<td colspan="2" style="max-width:400px;word-wrap:break-word;">
						==!<?php echo $line["payment_code"]; ?>
					</td>
				</tr>
				<tr>
					<td colspan="3">
						<b>Payment Instructions:</b> Please pay by transfering the funds to the "Agony Unleashed" corporation in game.  The Payment code must be included <b>exactly as printed above</b> in the "Reason" field when making the transfer.  From what character the transfer originates is irrelevant, any transfer with your Payment Code will be attributed to your payment for this class.
					</td>
				</tr>
					
	<?php 
	}
	?>
				
<?php 
}
?>
				<tr>
					<td align="left" colspan="3"><?php echo htmlspecialchars(stripslashes($line["comments"]));?></td>
				</tr>
				<tr>
					<td valign="top" align="right"><h4>Class Sessions</h4></td>
					<td colspan="2">
					
					
					
					
					
					
					
					

							<table class="subform">
								<?php 
								$j=0;
								$session_result = mysql_query("SELECT pkey, UNIX_TIMESTAMP(start_datetime) as start_datetime, duration, comments 
									FROM sessions WHERE class=".$line["classes_pkey"]." ORDER BY start_datetime ASC", $class_manager_db) or die(mysql_error($class_manager_db));
								while ($session_line = mysql_fetch_array($session_result, MYSQL_ASSOC)) {
								?>
									<tr>
										<td><hr></td>
									</tr>
									<tr>
										<td>Session <?php echo ($j+1);?> starts <b><?php echo date("d M Y H:i",$session_line["start_datetime"]);?></b> and will last for <b><?php echo $session_line["duration"];?></b> hour(s).</td>
									</tr>
									<tr>
										<td align="left">
											<?php echo htmlspecialchars(stripslashes($session_line["comments"]));?>
										</td>
									</tr>
								<?php
								$j++;
								}
								?>
									<tr>
										<td><hr></td>
									</tr>
							</table>
						
						
						
						
						
						
						
						
					</td>
				</tr>
				
				
				
				
				







			</table>
				
				
				
				
				
				
				
				
		</div>
	</td></tr>
<?php
	$i++;
}
?>
	<tr><td colspan="3"><hr></td></tr>
</table>
<?php
}
?>
</div>